package com.seqb.manage.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.seqb.common.ResultVo;
import com.seqb.common.mybatis.WrapperHandler;
import com.seqb.exception.BdException;
import com.seqb.manage.file.service.FileService;
import com.seqb.manage.video.dao.VideoDao;
import com.seqb.manage.video.domain.VideoDO;
import com.seqb.manage.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Arrays;
import java.util.Map;

/**
 * @author lilinshen
 * @title 视频表
 * @description 请填写相关描述
 * @date 2020-05-15
 */
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoDao videoDao;
    @Autowired
    private FileService fileService;

    @Override
    public VideoDO get(Long id) {
        return videoDao.selectById(id);
    }

    @Override
    public Page<VideoDO> list(Map<String, Object> map) {
        if (null != map && null != map.get("pageNumber") && null != map.get("pageSize")) {
            PageHelper.startPage(Integer.valueOf(map.get("pageNumber").toString()), Integer.valueOf(map.get("pageSize").toString()));
        } else {
            PageHelper.startPage(1, Integer.MAX_VALUE);
        }
        QueryWrapper queryWrapper = WrapperHandler.genQueryWrapper(map, VideoDO.class);
        queryWrapper.orderByAsc("id");
        return (Page<VideoDO>) videoDao.selectList(queryWrapper);
    }

    @Override
    public int count(Map<String, Object> map) {
        QueryWrapper queryWrapper = WrapperHandler.genQueryWrapper(map, VideoDO.class);
        return videoDao.selectCount(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(VideoDO video) {
        int insert = 0;
        try {
            insert = videoDao.insert(video);
            if (insert <= 0) {
                throw new BdException("操作失败！");
            }
            //更新附件表数据
            Long id = video.getId();
            Long fileUrlFileId = video.getFileUrlFileId();
            if (null != fileUrlFileId) {
                ResultVo resultVo = fileService.updateIds(fileUrlFileId + "", id, "VideoDO", "fileUrl");
                if (null != resultVo.get("fileUrl")) {
                    VideoDO videoDO = new VideoDO();
                    videoDO.setId(id);
                    videoDO.setFileUrl(String.valueOf(resultVo.get("fileUrl")));
                    int update = videoDao.updateById(videoDO);
                    if (update <= 0) {
                        throw new BdException("操作失败！");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return insert;
    }

    @Override
    @Transactional
    public int update(VideoDO video) {
        return videoDao.updateById(video);
    }

    @Override
    @Transactional
    public int remove(Long id) {
        return videoDao.deleteById(id);
    }

    @Override
    @Transactional
    public int batchRemove(Long[] ids) {
        return videoDao.deleteBatchIds(Arrays.asList(ids));
    }

}